<template>
  <div class="app-container dialog-w80">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-date-picker
            v-model="pickerDate"
            value-format="yyyy.MM.dd"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select filterable v-model="formInline.store_id" style="width:160px">
            <el-option
              v-for="item in store_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.type" filterable style="width:160px" @change="typeChange">
            <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.status" filterable style="width:160px">
            <el-option v-for="item in status" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.order_no"
            placeholder="请输入采购单号"
            @keyup.enter.native="searchClick"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table highlight-current-row :data="list" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status==1">待神采审核</span>
            <span v-if="scope.row.status==2">待平台确认</span>
            <span v-if="scope.row.status==3">审核关闭</span>
            <span v-if="scope.row.status==4">出库关闭</span>
            <span v-if="scope.row.type==2&&scope.row.status==5">待发货</span>
            <span v-if="scope.row.type==1&&scope.row.status==5">待其他平台发货</span>
            <span v-if="scope.row.type==2&&scope.row.status==6">其他平台待收货</span>
            <span v-if="scope.row.type==1&&scope.row.status==6">待收货</span>
            <!-- <span v-if="scope.row.status==7">待收货</span> -->
            <span v-if="scope.row.type==1&&scope.row.status==8">部分收货</span>
            <span v-if="scope.row.type==2&&scope.row.status==8">其他平台部分收货</span>
            <span v-if="scope.row.status==9">订单完成</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="本平台订单编号">
          <template slot-scope="scope">
            <el-button
              @click="get_order_info(scope.row,scope.$index)"
              type="text"
              size="small"
            >{{scope.row.order_no}}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="其他平台订单编号" prop="platform_order_no"></el-table-column>
        <el-table-column align="center" label="调拨出库仓" prop="store_out_name"></el-table-column>
        <el-table-column align="center" label="调拨入库仓" prop="store_in_name"></el-table-column>
        <el-table-column align="center" label="下单时间" prop="create_time"></el-table-column>
        <el-table-column align="center" label="备注" prop="remark"></el-table-column>
      </el-table>
    </div>
    <!-- 礼品调拨入库弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      class="dialog_table"
      :title="formInline.type==1 ?'礼品调拨入库':'礼品调拨出库'"
      :visible.sync="detail_dialog"
    >
      <div class="dialogInContainer" v-loading="dialogLoading">
        <el-table
          highlight-current-row
          :data="infolist"
          v-loading.body="listLoading"
          border
          size="small"
          @selection-change="detailSelection"
        >
          <el-table-column v-if="formInline.type==1" type="selection" width="36"></el-table-column>
          <el-table-column align="center" label="序号" width="50">
            <template slot-scope="scope">
              <span class="indexColor">{{scope.$index+1}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="图片" width="70">
            <template slot-scope="scope">
              <img :src="scope.row.thumb" width="50" height="50" alt />
            </template>
          </el-table-column>
          <el-table-column align="center" label="礼品名称" prop="gift_name"></el-table-column>
          <el-table-column align="center" label="新礼品编号" prop="gift_id"></el-table-column>
          <el-table-column align="center" label="神采礼品编号" prop="gift_id"></el-table-column>
          <el-table-column align="center" label="箱规" prop="box_num"></el-table-column>
          <el-table-column align="center" label="尺寸" prop="size"></el-table-column>
          <el-table-column align="center" label="三级分类" prop="type_name"></el-table-column>
          <el-table-column align="center" label="成本价" prop="money_cost"></el-table-column>
          <el-table-column align="center" label="订单数" prop="order_num"></el-table-column>
          <template v-if="type==1">
            <el-table-column align="center" label="累计到货数" prop="arrive_num"></el-table-column>
            <el-table-column align="center" label="本次到货数">
              <template slot-scope="scope">
                <el-input
                  type="number"
                  onmousewheel="return false;"
                  v-model="scope.row.now_arrive_num"
                  :disabled="isDisabled||scope.row.is_del*1>0||scope.row.all_arrive"
                  placeholder="数量"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column align="center" label="差异">
              <template slot-scope="scope">
                <span
                  :class=" scope.row.now_arrive_num*1+scope.row.arrive_num*1 -scope.row.order_num*1==0?'':'danger'"
                >
                  {{scope.row.now_arrive_num*1+scope.row.arrive_num*1
                  -scope.row.order_num*1 }}
                </span>
              </template>
            </el-table-column>
          </template>
        </el-table>
        <!-- 自定义底部 -->
        <br />
        <div class="bottom_box">
          <el-button
            v-if="infoSelection.length>0 && !isDisabled"
            :disabled="isDisabled"
            size="small"
            type="primary"
            @click="allArriveClick()"
          >无差异到货</el-button>
          <el-button
            v-if="infoSelection.length>0 && !isDisabled"
            :disabled="isDisabled"
            size="small"
            type="primary"
            @click="resetClick()"
          >重置</el-button>
          <el-button
            class="pull-right"
            :disabled="isDisabled"
            size="small"
            type="primary"
            :loading="btnLoding"
            @click="saveClick()"
          >确认提交</el-button>
          <p class="pull-right" style="line-height:0">
            礼品总数量: {{totalOrder}}
            礼品总金额: {{totalPrice|keepTwoNum}} &#x3000;
          </p>
        </div>
      </div>
    </el-dialog>

    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getStoreList,
  getList,
  getPage,
  getinfoList,
  save,
} from "@/api/storage_control/kua_out_in";
import { pickerOptionsF, pickerDateF, optionMaxTime } from "@/utils";
export default {
  data() {
    return {
      type: "1",
      btnLoding: false,
      dialogLoading: false,
      listLoading: false,
      type_list: [
        { id: 1, name: "其他平台调入订单" },
        { id: 2, name: "神采平台调出订单" },
      ],
      status: [
        { id: -1, name: "全部" },
        { id: 1, name: "待神采审核" },
        { id: 2, name: "待其他平台确认" },
        { id: 3, name: "审核关闭" },
        { id: 4, name: "出库关闭" },
        { id: 5, name: "待发货" },
        { id: 6, name: "其他平台待收货" },
        // { id: 7, name: "待收货" },
        { id: 8, name: "其他平台部分收货" },
        { id: 9, name: "订单完成" },
      ],
      currentPage: 1,
      returnApplyDialog: false,
      totalDataNumber: 0,
      returnInfoDialog: false,
      returnInfoList: [],
      logistics_company: "",
      logistics_no: "",
      editId: "",
      editIndex: "",
      return_remark: "",
      stateName: "",
      formInline: {
        store_id: "",
        order_no: "",
        start_time: "",
        end_time: "",
        status: -1,
        type: 2,
      },
      detail_dialog: false,
      infolist: [],
      store_list: null,
      returnApplyList: [],
      list: null,
      saveForm: {
        order_id: this.editId,
        order: [],
      },
      pickerOptions: pickerOptionsF(),
      pickerDate: pickerDateF(),
      exportForm: "",
      infoSelection: [],
      isDisabled: false,
      totalOrder: "",
      totalPrice: "",
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.store_list) {
        vm.getstore();
      }
    });
  },
  methods: {
    typeChange() {
      this.status.map((item) => {
        if (item.id == 5) {
          item.name = this.formInline.type == 2 ? "待发货" : "待其他平台发货";
        }
        if (item.id == 6) {
          item.name = this.formInline.type == 2 ? "其他平台待收货" : "待收货";
        }
        if (item.id == 8) {
          item.name =
            this.formInline.type == 2 ? "其他平台部分收货" : "部分收货";
        }
      });
    },
    detailSelection(val) {
      this.infoSelection = val;
    },
    // 计算购物车
    count() {
      var totalPrice = 0; //临时总价
      var totalOrder = 0;
      this.infolist.forEach(function (val, index) {
        totalPrice += val.order_num * 1 * val.money_cost * 1; //累计总价
        totalOrder += val.order_num * 1;
      });
      this.totalPrice = parseFloat(totalPrice);
      this.totalOrder = parseFloat(totalOrder);
    },
    resetClick() {
      this.infoSelection.forEach((val1) => {
        this.infolist.forEach((val2) => {
          if (val1.id == val2.id) {
            val2.now_arrive_num = "";
          }
        });
      });
    },
    allArriveClick() {
      this.infoSelection.forEach((val1) => {
        this.infolist.forEach((val2) => {
          if (val1.id == val2.id) {
            val2.now_arrive_num = 0;
            val2.now_arrive_num =
              val2.order_num * 1 - val2.arrive_num * 1 == 0
                ? ""
                : val2.order_num * 1 - val2.arrive_num * 1;
          }
        });
      });
    },
    saveClick() {
      let flat = true;
      this.saveForm.order = [];
      // 过滤掉全部到货的数组
      let new_infolist = this.infolist.filter(function (item) {
        return item.order_num != item.arrive_num;
      });
      this.infolist.forEach((val, index) => {
        if (
          (!val.all_arrive && val.is_del == 0 && val.now_arrive_num == "") ||
          (!val.all_arrive && val.is_del == 0 && val.now_arrive_num == null)
        ) {
          return;
        } else {
          if (
            Number(val.arrive_num) + Number(val.now_arrive_num) >
            Number(val.order_num)
          ) {
            flat = false;
            // return;
          } else {
            this.saveForm.order.push({
              gift_id: val.gift_id,
              arrive_num: val.now_arrive_num * 1,
              look_money: val.money_sell,
            });
          }
        }
      });
      if (flat == false) {
        this.$message({ type: "error", message: "到货数不能大于订单数!" });
        return;
      }
      if (this.saveForm.order.length == this.infolist.length) {
        this.$confirm("此操作提交, 是否继续?", "提示", {
          type: "warning",
        }).then(() => {
          this.btnLoding = true;
          save(this.saveForm, this.infolist[0].type)
            .then((response) => {
              this.$message({
                type: response.code == 1 ? "success" : "error",
                message: response.message,
              });
              if (response.code == 1) {
                this.btnLoding = false;
                this.detail_dialog = false;
                this.handleCurrentChange(this.currentPage);
              }
            })
            .catch(() => {
              this.btnLoding = false;
            });
        });
      } else {
        this.$message({
          type: "error",
          message: "请将所有信息填写完整（未到货的请填0）",
        });
        return;
      }
    },
    // 打开订单详情
    get_order_info(row, index) {
      this.type = row.type;
      this.dialogLoading = true;
      this.detail_dialog = true;
      this.saveForm.order_id = row.order_id;
      getinfoList({ order_id: row.order_id }).then((response) => {
        if (row.status == 5 && row.type == 2) {
          response.map((item) => {
            return (item.now_arrive_num = item.order_num);
          });
        }
        response.forEach((val) => {
          val.all_arrive = val.order_num == val.arrive_num ? true : false;
        });
        this.infolist = response;
        if (
          (row.status == 6 && row.type != 2) ||
          (row.status == 8 && row.type != 2) ||
          (row.status == 5 && row.type == 2)
        ) {
          this.isDisabled = false;
        } else {
          this.isDisabled = true;
        }
        this.dialogLoading = false;
        this.count();
      });
    },
    getstore() {
      getStoreList({ category_id: this.$route.meta.id }).then((response) => {
        this.store_list = response;
        let data = response.filter(
          (res) => res.id == this.$store.getters.optionId
        );
        if (data.length > 0) {
          this.formInline.store_id = data[0].id;
        }
      });
    },
    searchClick() {
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.start_time = this.pickerDate ? this.pickerDate[0] : "";
        this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : "";
        this.fetchData();
      });
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then((response) => {
          this.totalDataNumber = response * 20;
          getList(this.formInline).then((response) => {
            this.list = response;
            this.listLoading = false;
          });
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then((response) => {
        this.list = response;
        this.listLoading = false;
      });
    },
  },
};
</script>